<?php
/**
 * 作用：支付订单
 * 作者: sunwgjj@126.com
 * 日期: 2016/6/6 14:05
 * 公司: 浙江宣逸网络科技有限公司<www.soe-soe.com>
 */
namespace Common\Model;
class OrderModel extends BaseModel{
	//订单类型
	public static $orderTypeInfo = array(
		OBJ_TYPE_ACTIVITY       =>"getEventsInfo",//活动
		OBJ_TYPE_POINTMALL      =>"getPointMallInfo",//兑换
	);
	/**
	 * 获取订单信息
	 * User: Rick.Sun.<sunwgjj@126.com>
	 * Return: array
	 * Address: 浙江宣逸网络科技有限公司
	 * @param $id
	 * @param $uid
	 * @return array
	 */
	public static function getOrderInfo($id,$uid=0){
		$dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
		$dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');

		if(!$id){
			return $dataFailure;
		}
		$where="id={$id}";
		if($uid>0){
			$where .=" and uid={$uid}";
		}
		$info = M("o_order",null,DB_MAIN_CFG)->where($where)->find();

		if(!$info) return $dataFailure;
		$dataSuccess['data']=$info;
		return $dataSuccess;
	}

	/**
	 * 重新更新订单
	 * User: Rick.Sun.<sunwgjj@126.com>
	 * Return: array
	 * Address: 浙江宣逸网络科技有限公司
	 * @param $id
	 * @param $uid
	 * @param $data
	 * @return array
	 */
	public static function updateOrderInfo($id, $uid, $data){
		$dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
		$dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');

		if(!$id || !$uid){
			return $dataFailure;
		}

		M("o_order",null,DB_MAIN_CFG)->where("id={$id} and uid={$uid}")->save($data);
		return self::getOrderInfo($id, $uid);
	}

	/**
	 * * 更新对应 对象 状态
	 * User: Rick.Sun.<sunwgjj@126.com>
	 * Return: array
	 * Address: 浙江宣逸网络科技有限公司
	 * @param $info
	 * @return array
	 */
	public static function updateObjStatus($info){
		$dataSuccess = array('status'=>self::SUCCESS,'msg'=>'成功');
		$dataFailure = array('status'=>self::FAILURE,'msg'=>'无效参数');

		if(!$info){
			return $dataFailure;
		}

		switch ($info['objType']){
			case OBJ_TYPE_ACTIVITY:
				EventsModel::afterPay($info['id']);
				break;
			case OBJ_TYPE_POINTMALL:
				PointMallModel::afterPay($info['id'],0);
				break;
		}

		return $dataSuccess;

	}

	/**
	 * 设置 余额支付状态
	 * User: Rick.Sun.<sunwgjj@126.com>
	 * Return: void
	 * Address: 浙江宣逸网络科技有限公司
	 * @param $id
	 * @param $uid
	 */
	public static function updatePayBalance($id, $uid){
		M("o_order",null,DB_MAIN_CFG)->where("id={$id} and uid={$uid}")->save( array('payTime'=>time() ) );
	}

	/**
	 * 创建新订单
	 * User: Rick.Sun.<sunwgjj@126.com>
	 * Return: mixed
	 * Address: 浙江宣逸网络科技有限公司
	 * @param $data (uid,objType,objID,orderTitle,orderDesc,point,pay,marketPrice,userName,phone,address,payEndTime)
	 * @return mixed
	 */
	public static function buildOrder($data){
		$insertData = array();
		$insertData['outTradeNo'] = self::getOrderOutTradeNo($data['objType']);
		$insertData['addTime'] 		= time();
		$insertData['uid'] 			= $data['uid'];
		$insertData['objType'] 		= $data['objType'];
		$insertData['objID'] 		= $data['objID'];
		$insertData['orderTitle'] 	= $data['orderTitle'];
		$insertData['orderDesc'] 	= $data['orderDesc'];
		$insertData['point'] 		= $data['point'];
		$insertData['pay'] 			= $data['pay'];
		$insertData['marketPrice'] 	= $data['marketPrice'];
		$insertData['userName'] 	= $data['userName'];
		$insertData['phone'] 		= $data['phone'];
		$insertData['address'] 		= $data['address'];
		$insertData['payEndTime'] 	= $data['payEndTime'];
		$insertid = M("o_order",null,DB_MAIN_CFG)->add( $insertData );
		return $insertid;
	}

	/**
	 * 生成新订单号
	 * User: Rick.Sun.<sunwgjj@126.com>
	 * Return: string
	 * Address: 浙江宣逸网络科技有限公司
	 * @return string
	 */
	private static function getOrderOutTradeNo($objType){
		//对象类型，发生时间，随机数
		$outTradeNo = $objType.'_'.time().'_'.rand(1,1000);
		return $outTradeNo;
	}

	
}